package projcet.test.student.student;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdateActivity extends AppCompatActivity {


    @BindView(R.id.addViewTop)
    TextView addViewTop;
    @BindView(R.id.backList)
    ImageButton backList;
    @BindView(R.id.addText1)
    EditText addText1;
    @BindView(R.id.line1)
    LinearLayout line1;
    @BindView(R.id.addText2)
    EditText addText2;
    @BindView(R.id.line2)
    LinearLayout line2;
    @BindView(R.id.addText3)
    EditText addText3;
    @BindView(R.id.line3)
    LinearLayout line3;
    @BindView(R.id.addText4)
    EditText addText4;
    @BindView(R.id.line5)
    LinearLayout line5;
    @BindView(R.id.spn_subjet)
    Spinner spnSubjet;
    @BindView(R.id.line4)
    LinearLayout line4;
    @BindView(R.id.submitBtn2)
    Button submitBtn2;
    private DbOpenHelper mHelper;

    StudentInfo studentInfo;
    List<Info> mData;

    List<String> teachers;

    private ArrayAdapter adapter = null;
    int po = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        ButterKnife.bind(this);
        mHelper = new DbOpenHelper(this);
        mData = new ArrayList<>();
        teachers = new ArrayList<>();
        getStudentInfoList();
        for (int i = 0; i < mData.size(); i++) {
            teachers.add(mData.get(i).getSubject());
        }
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, teachers);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spnSubjet.setAdapter(adapter);
        Intent intent = getIntent();
        studentInfo = (StudentInfo) intent.getSerializableExtra("student");
        if (studentInfo != null) {
            addText1.setText(studentInfo.getName());
            addText2.setText(studentInfo.getClassName());
            addText3.setText(studentInfo.getStudentNo());
            addText4.setText(studentInfo.getTotal() + "");
            for (int i = 0; i < mData.size(); i++) {
                studentInfo.getSubject().equals(mData.get(i).getSubject());
                po = i;
            }


            spnSubjet.setSelection(po);
        }
////        mUpdateChinese.setText(String.valueOf(intent.getIntExtra("chinese", 0)));
//        addText4.setText(String.valueOf(intent.getIntExtra("math", 0)));

        //回到列表页
        backList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(UpdateActivity.this, ListActivity.class);
                startActivity(intent);
                finish();
            }
        });

        //保存数据

        saveData();
    }

    /**
     * 保存数据
     */
    public void saveData() {


        submitBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String name = addText1.getText().toString();
                final String className = addText2.getText().toString();
                final String studentNo = addText3.getText().toString();
                final String score = addText4.getText().toString();
                final String subject = spnSubjet.getSelectedItem().toString();


                SQLiteDatabase db = mHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("name", name);
                values.put("className", className);
                values.put("subject", subject);
                values.put("score", score);

                String[] args = {String.valueOf(studentNo)};
                db.update("Student", values, "studentNo = ?", args);
                values.clear();
                db.close();
                Intent intent = new Intent(UpdateActivity.this, ListActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * 获得数据库中的数据
     */
    private List<Info> getStudentInfoList() {
        mData = new ArrayList<Info>();
        SQLiteDatabase sqLiteDatabase = mHelper.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query("Teacher", null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                String teacher = cursor.getString(cursor.getColumnIndex("teacher"));
                String subject = cursor.getString(cursor.getColumnIndex("subject"));


                mData.add(new Info(teacher, subject));
            } while (cursor.moveToNext());
        }
        cursor.close();

        return mData;
    }
}
